// 存储所有标记的id
let _ids: any[] = [];
// 存储标点对应的上一个位置
export let markerInfo: any = {};
// 动画时长
export const ANIMATION_DURATION = 1000;
// 航迹线最大点数
export const MAX_TRAIL_POINTS = 20000;
// 相机更新间隔（毫秒）
export const CAMERA_UPDATE_INTERVAL = 100;
// 是否启用追踪
let _isTrackingEnabled = false;
// 当前追踪的标记 ID
let _trackingId: string | null = null;
// 上次相机更新时间
let _lastCameraUpdateTime = 0;

// 获取所有标记的id
export const getIds = () => _ids;
// 设置所有标记的id
export const setIds = (newIds: any[]) => {
  _ids = newIds;
};
// 获取是否启用追踪
export const getIsTrackingEnabled = () => _isTrackingEnabled;
// 设置是否启用追踪
export const setIsTrackingEnabled = (value: boolean) => {
  _isTrackingEnabled = value;
};
// 获取当前追踪的标记 ID
export const getTrackingId = () => _trackingId;
// 设置当前追踪的标记 ID
export const setTrackingId = (id: string | null) => {
  _trackingId = id;
};
// 获取上次相机更新时间
export const getLastCameraUpdateTime = () => _lastCameraUpdateTime;
// 设置上次相机更新时间
export const setLastCameraUpdateTime = (time: number) => {
  _lastCameraUpdateTime = time;
};

// 线性插值函数
export const lerp = (start: [number, number], end: [number, number], fraction: number): [number, number] => {
  return [
    start[0] + (end[0] - start[0]) * fraction,
    start[1] + (end[1] - start[1]) * fraction,
  ];
};

// 计算朝向的函数
export const calculateBearing = (start: [number, number], end: [number, number]): number => {
  const startLng = (start[0] * Math.PI) / 180;
  const startLat = (start[1] * Math.PI) / 180;
  const endLng = (end[0] * Math.PI) / 180;
  const endLat = (end[1] * Math.PI) / 180;

  const y = Math.sin(endLng - startLng) * Math.cos(endLat);
  const x =
    Math.cos(startLat) * Math.sin(endLat) -
    Math.sin(startLat) * Math.cos(endLat) * Math.cos(endLng - startLng);
  const bearing = (Math.atan2(y, x) * 180) / Math.PI;
  return (bearing + 360) % 360;
};

// 计算点到线的垂直距离
export const perpendicularDistance = (point: [number, number], lineStart: [number, number], lineEnd: [number, number]): number => {
  const [x, y] = point;
  const [x1, y1] = lineStart;
  const [x2, y2] = lineEnd;

  const numerator = Math.abs((y2 - y1) * x - (x2 - x1) * y + x2 * y1 - y2 * x1);
  const denominator = Math.sqrt(Math.pow(y2 - y1, 2) + Math.pow(x2 - x1, 2));
  return numerator / denominator;
};

// 简化航迹线（Douglas-Peucker 算法）
export const simplifyTrail = (trail: [number, number][], tolerance: number = 0.0001): [number, number][] => {
  if (trail.length <= 2) return trail;

  // 找到离起点和终点连线最远的点
  let maxDistance = 0;
  let maxIndex = 0;
  const start = trail[0];
  const end = trail[trail.length - 1];

  for (let i = 1; i < trail.length - 1; i++) {
    const distance = perpendicularDistance(trail[i], start, end);
    if (distance > maxDistance) {
      maxDistance = distance;
      maxIndex = i;
    }
  }

  // 如果最远点大于容差，则递归简化
  if (maxDistance > tolerance) {
    const left = simplifyTrail(trail.slice(0, maxIndex + 1), tolerance);
    const right = simplifyTrail(trail.slice(maxIndex), tolerance);
    return left.slice(0, -1).concat(right);
  } else {
    return [start, end];
  }
};


// 实时飞行数据展示
export const realTimeLayer = (options: any) => {

  if (JSON.stringify(options) == "{}") {
      return
  }
  return `<div style="
    background: #FFFFFF;
    border-radius: 6px;
    padding: 6px;
    min-width: 150px;
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
    font-family: Arial, sans-serif;
  ">
    <div style="
      display: flex;
      align-items: center;
      gap: 4px;
      margin-bottom: 6px;
      padding-bottom: 4px;
      border-bottom: 1px solid #F0F0F0;
    ">
      <div style="
        background: #437EF1;
        color: white;
        padding: 1px 4px;
        border-radius: 3px;
        font-size: 10px;
      ">飞行中</div>
      <div style="
        color: #437EF1;
        font-size: 11px;
        font-weight: 500;
      ">${options.modelName || 'B3836'}</div>
    </div>
    <div style="display: grid; grid-template-columns: 1fr 1fr; text-align: center;">
      <div>
        <div style="color: #909399; font-size: 9px;">海拔高度</div>
        <div style="color: #437EF1; font-size: 11px; font-weight: 500; font-family: DIN;">
          ${options.coordinates[2] || '0'}<span style="font-size: 9px; color: #909399; margin-left: 1px;">米</span>
        </div>
      </div>
      <div>
        <div style="color: #909399; font-size: 9px;">飞行速度</div>
        <div style="color: #437EF1; font-size: 11px; font-weight: 500; font-family: DIN;">
        </div>
      </div>
      <div>
        <div style="color: #909399; font-size: 9px;">经度</div>
        <div style="color: #437EF1; font-size: 11px; font-weight: 500; font-family: DIN;">
          ${Number(options.coordinates[0]).toFixed(6) || '0'}
        </div>
      </div>
      <div>
        <div style="color: #909399; font-size: 9px;">纬度</div>
        <div style="color: #437EF1; font-size: 11px; font-weight: 500; font-family: DIN;">
          ${Number(options.coordinates[1]).toFixed(6) || '0'}
        </div>
      </div>
    </div>
  </div>`

}